package com.ironone.streaming;

import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SlidingPaneLayout;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ironone.streaming.application.CusAlrtBtnTypes;
import com.ironone.streaming.application.CusAlrtIconTypes;
import com.ironone.streaming.application.CustomAlertDialog;
import com.ironone.streaming.application.FormObject;
import com.ironone.streaming.application.MainApplication;
import com.ironone.streaming.lite.R;
import com.ironone.streaming.model.EventsDataModel;

public class EventsFragment extends Fragment {
	View view;
	SlidingPaneLayout mPane;
	Button btnMenu;

	ImageView albumart_bg;
	Bitmap bitmap;

	//Custom view: custom_imageview.xml
	TextView text_display, text_link;
	View image_view_overlay;

	LinearLayout image_container, background_color_layer;
	ImageView logo;


	@Override
	public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.events_fragment, container, false);

		try {
			mPane = (SlidingPaneLayout) getActivity().findViewById(R.id.pane);
			btnMenu = (Button) view.findViewById(R.id.btnMenu);
			btnMenu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mPane.openPane();
				}
			});

			// Menu btn
			int scrnWidth = FormObject.getInstance().getScreenWidth();
			int supWidth = (int)((scrnWidth * 0.6));
			RelativeLayout.LayoutParams abParam = (RelativeLayout.LayoutParams) btnMenu.getLayoutParams();
			abParam.width = (int)(supWidth - (supWidth * 0.88));
			abParam.height = (int)(supWidth - (supWidth * 0.88));
			btnMenu.setLayoutParams(abParam);
			
			//Logo
			logo=(ImageView) view.findViewById(R.id.imageView1);
			RelativeLayout.LayoutParams logoParam = (RelativeLayout.LayoutParams) logo.getLayoutParams();
			logoParam.width = (int) (supWidth - (supWidth * 0.62));
			logoParam.height = (int) (supWidth - (supWidth * 0.62));
			logo.setLayoutParams(logoParam);
			
			// Scroll view
			//Helani : Changed to horizontal
			ScrollView scrollview = (ScrollView) view.findViewById(R.id.scrollview);
			//HorizontalScrollView scrollview = (HorizontalScrollView) view.findViewById(R.id.scrollview);
			RelativeLayout.LayoutParams ayParam = (RelativeLayout.LayoutParams) scrollview.getLayoutParams();
			//ayParam.width = (int)((scrnWidth * 0.8));
			//ayParam.height = (int)((scrnWidth));
			scrollview.setLayoutParams(ayParam);
			scrollview.setSmoothScrollingEnabled(true);
			scrollview.setAlwaysDrawnWithCacheEnabled(true);
			
			background_color_layer = (LinearLayout) view.findViewById(R.id.background_color_layer);
			//background_color_layer.getBackground().setAlpha(96);
			
			// Background overlay image
			/*bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_frag);
			albumart_bg = (ImageView) view.findViewById(R.id.albumart_bg);

			ViewTreeObserver vto = albumart_bg.getViewTreeObserver();
			vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					albumart_bg = (ImageView) view.findViewById(R.id.albumart_bg);
					if(FormObject.getInstance().getAlbumArtImage() == null)
						albumart_bg.setImageBitmap(bitmap);
					else
						albumart_bg.setImageBitmap(FormObject.getInstance().getAlbumArtImage());

					albumart_bg.setAlpha(96);
				}
			});
*/
			image_container = (LinearLayout) view.findViewById(R.id.image_container);
			List<EventsDataModel> eventModel = FormObject.getInstance().getEventsData();

			StringBuilder sb = new StringBuilder();
			String more = getString(R.string.more);

			for(int x=0; x < eventModel.size(); x++) {
				View convertView = inflater.inflate(R.layout.custom_imageview, null);	
				ImageView image_view = (ImageView) convertView.findViewById(R.id.image_view);
				image_view_overlay = (View) convertView.findViewById(R.id.image_view_overlay);
				text_display = (TextView) convertView.findViewById(R.id.text_display);
				text_link = (TextView) convertView.findViewById(R.id.text_link);

				if(eventModel.get(x).image == null) continue;

				//if(!showModel.get(x).showDays[MainApplication.getDayIndex()])
				//continue;

				int imgW = MainApplication.getByteArrToBitmap(eventModel.get(x).image).getWidth();
				int imgH = MainApplication.getByteArrToBitmap(eventModel.get(x).image).getHeight();

				//ImageView image = new ImageView(getActivity());
				//http://stackoverflow.com/questions/15240977/how-to-add-dynamic-row-view-in-scrollview-in-android
				convertView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
												
						TextView tv = (TextView) v.findViewById(R.id.text_display);			//.findViewById(R.id.text_display);
						TextView tl = (TextView) v.findViewById(R.id.text_link);
						image_view_overlay = (View) v.findViewById(R.id.image_view_overlay);

						if(tv.getVisibility() == View.VISIBLE){
							tv.setVisibility(View.INVISIBLE);
							tl.setVisibility(View.INVISIBLE);
							image_view_overlay.setVisibility(View.INVISIBLE);
						} else{
							tv.setVisibility(View.VISIBLE);
							tl.setVisibility(View.VISIBLE);
							image_view_overlay.setVisibility(View.VISIBLE);	
							resetUi(v);
						}
					}
				});

				RelativeLayout.LayoutParams pam = (RelativeLayout.LayoutParams) image_view.getLayoutParams();
				pam.width = (int)(scrnWidth * 1.0); //0.8
				float ss = (float)(imgW * 1.0 / pam.width * 1.0);
				pam.height = (int)(imgH / ss);
				//Helani
				// Calculation based on layout weights in events and radio player
				int scrnheight = FormObject.getInstance().getScreenHeight();
				//pam.height = (int)(scrnheight * ((8 * 6)/((10.65+0.5) * 7)) );
				pam.height = (int)(scrnheight * ((8 * 5.3)/((10.65+0.5) * 7)) );
				image_view.setLayoutParams(pam);

				image_view.setImageBitmap(MainApplication.getByteArrToBitmap(eventModel.get(x).image));
				text_display.setTextSize(FormObject.getInstance().getTextSize());
				text_display.setText(eventModel.get(x).eventName); // + "\n" + eventModel.get(x).eventtime_start + "-" + eventModel.get(x).eventtime_end);

				if(eventModel.get(x).webLink != null && !eventModel.get(x).webLink.equals("")){
					sb.setLength(0);
					sb.append("<a href=\"");
					sb.append(eventModel.get(x).webLink);
					sb.append("\">");
					sb.append(more);
					sb.append("</a>");
					text_link.setText(Html.fromHtml(sb.toString()));
					text_link.setMovementMethod(LinkMovementMethod.getInstance());
				}				
				image_container.addView(convertView);
			}
		} catch (Exception e) {
			CustomAlertDialog alertDialog = new CustomAlertDialog(getActivity(), CusAlrtBtnTypes.OK, CusAlrtIconTypes.DUCK);
			alertDialog.setTitleText(getString(R.string.what_the_duck_));
			//alertDialog.setMessageText(getString(R.string.something_snapped));			
			alertDialog.show();
		}
		return view;
	}

	private void resetUi(View vx){
		int childcount = image_container.getChildCount();
		for (int i=0; i < childcount; i++){
			View v = image_container.getChildAt(i);
			
			if(v.equals(vx)) continue;
				
			TextView tv = (TextView) v.findViewById(R.id.text_display);			//.findViewById(R.id.text_display);
			TextView tl = (TextView) v.findViewById(R.id.text_link);
			image_view_overlay = (View) v.findViewById(R.id.image_view_overlay);

			if(tv.getVisibility() == View.VISIBLE){
				tv.setVisibility(View.INVISIBLE);
				tl.setVisibility(View.INVISIBLE);
				image_view_overlay.setVisibility(View.INVISIBLE);
			}			
		}
	}
}
